package com.fido.kingkongview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.fido.kingkongview.holder.KHolder
import com.fido.kingkongview.holder.KHolderCreator
import com.fido.kingkongview.utils.width
import java.lang.RuntimeException

/**
 * @author: FiDo
 * @date: 2024/7/24
 * @des:
 */
class KingKongItemAdapter<T>(
    private val recyclerView: RecyclerView,
): RecyclerView.Adapter<KHolder<T>>() {

    private var dataList:List<T?>?=null

    private var mWidth = 0

    private var offScreenItemWidth = 0

    private var spanCount:Int = 4

    private var holderCreator: KHolderCreator<T>?=null

    private var onItemClickListener:((data:T?,pos:Int)->Unit)?=null

    private var itemHeight = 0

    fun setHolderCreator(holderCreator: KHolderCreator<T>) = apply {
        this.holderCreator = holderCreator
    }

    fun setOnItemClickListener(listener: (data:T?,pos:Int)->Unit) = apply {
        this.onItemClickListener = listener
    }

    fun setSpanCount(span:Int) = apply {
        spanCount = span
    }

    fun setOffScreenWidth(offScreenItemWidth:Int) = apply {
        this.offScreenItemWidth = offScreenItemWidth
    }

    fun onWidthChange(width:Int){
        mWidth = width
        notifyDataSetChanged()
    }

    fun setData(data: List<T?>?){
        dataList = data
        notifyDataSetChanged()
    }

    fun updateItemHeight(height:Int){
        itemHeight = height
        notifyDataSetChanged()
    }

    private fun getRealItemWidth():Int{
        val width = if (mWidth == 0) recyclerView.measuredWidth else mWidth
        return width / spanCount - (if (offScreenItemWidth>0) offScreenItemWidth/spanCount else 0)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KHolder<T> {
        if (holderCreator == null) throw RuntimeException("please set holderCreator first")
        val layoutId = holderCreator!!.getLayoutId()
        val itemView = LayoutInflater.from(parent.context).inflate(layoutId,parent,false)
        val layoutParams = itemView.layoutParams
//        val width = if (mWidth == 0) recyclerView.measuredWidth else mWidth
//        layoutParams.width = width / spanCount - (if (offScreenItemWidth>0) offScreenItemWidth/spanCount else 0)
        layoutParams.width = getRealItemWidth()
        if (itemHeight > 0) {
            layoutParams.height = itemHeight
        }
        itemView.layoutParams = layoutParams
        return holderCreator!!.creatorHolder(itemView)
    }

    override fun getItemCount(): Int {
        return dataList?.size?:0
    }

    override fun onBindViewHolder(holder: KHolder<T>, position: Int) {
        if (dataList == null) return
        if (holder.itemView.width>0) {
            val realWidth = getRealItemWidth()
            if (realWidth > 0) {
                holder.itemView.width(realWidth)
            }
        }
        holder.itemView.visibility = if (dataList?.get(position) == null) View.INVISIBLE else View.VISIBLE
        holder.itemView.setOnClickListener {
            if (dataList?.get(position) != null) {
                onItemClickListener?.invoke(dataList?.get(position),position)
            }
        }
        holder.onBind(holder, dataList?.get(position),position)
    }
}